package com.kifas.controller;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;
import javax.xml.bind.JAXBElement;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.kifas.entity.BarangEntity;
import com.kifas.entity.GudangEntity;
import com.kifas.entity.JenisBarangEntity;
import com.kifas.entity.PenerimaanDetailEntity;
import com.kifas.entity.PenerimaanEntity;
import com.kifas.model.xml.BarangModel;
import com.kifas.model.xml.GudangModel;
import com.kifas.model.xml.JenisBarangModel;
import com.kifas.model.xml.PenerimaanDetailModel;
import com.kifas.model.xml.PenerimaanModel;
import com.kifas.model.xml.PersistStatus;
import com.kifas.model.xml.SatuanModel;
import com.kifas.service.EntityServiceManager;

@Component
@Scope("prototype")
@Path("/Penerimaans")
public class PenerimaanController {

	private static Logger log = Logger.getLogger(PenerimaanController.class);

	@Context
	UriInfo uriInfo;

	@Autowired
	private EntityServiceManager entityManager;

	@GET
	@Produces(MediaType.TEXT_XML)
	public List<PenerimaanModel> getPenerimaans() {
		return transformList(entityManager.getPenerimaans());
	}

	@POST
	public PersistStatus insert(JAXBElement<PenerimaanModel> xml) {
		PenerimaanModel penerimaanModel = xml.getValue();
		PenerimaanEntity penerimaan = new PenerimaanEntity();
		penerimaan.setIdPenerimaan(null);
		penerimaan.setIdPembelian(penerimaanModel.getIdPembelian());
		penerimaan.setKeterangan(penerimaanModel.getKeterangan());
		penerimaan.setNomorPenerimaan(penerimaanModel.getNomorPenerimaan());
		penerimaan.setNomorPo(penerimaanModel.getNomorPo());
		penerimaan.setTanggal(penerimaanModel.getTanggal());
		List<PenerimaanDetailEntity> penerimaanDetailEntities = new ArrayList<PenerimaanDetailEntity>();
		for (PenerimaanDetailModel penerimaanDetailModel : penerimaanModel.getPenerimaanDetailModels()) {
			PenerimaanDetailEntity penerimaanDetail = new PenerimaanDetailEntity();
			BarangEntity barang = entityManager.getBarang(penerimaanDetailModel.getBarang().getIdBarang());
			GudangEntity gudang = entityManager.getGudang(penerimaanDetailModel.getGudang().getIdMasterGudang());
			penerimaanDetail.setIdDetailPenerimaan(null);
			penerimaanDetail.setQty(penerimaanDetailModel.getQty());
			penerimaanDetail.setKeterangan(penerimaanDetailModel.getKeterangan());
			penerimaanDetail.setNoKontainer(penerimaanDetailModel.getNoKontainer());
			penerimaanDetail.setBulanKadaluarsa(penerimaanDetailModel.getBulanKadaluarsa());
			penerimaanDetail.setBarang(barang);
			penerimaanDetail.setGudang(gudang);
			penerimaanDetail.setPenerimaan(penerimaan);
			penerimaanDetailEntities.add(penerimaanDetail);
		}
		penerimaan.setPenerimaanDetails(new HashSet<PenerimaanDetailEntity>(penerimaanDetailEntities));
		try {
			entityManager.persist(penerimaan);
		} catch (Exception e) {
			log.error("can not persist penerimaan", e);
			return new PersistStatus("PenerimaanEntity", "failed", "can not persist penerimaan");
		}
		return new PersistStatus("PenerimaanEntity", "success");
	}

	@GET
	@Path("{id}")
	@Produces(MediaType.TEXT_XML)
	public PenerimaanModel getPenerimaan(@PathParam("id") Long idPenerimaan) {
		return transform(entityManager.getPenerimaan(idPenerimaan));
	}

	private List<PenerimaanModel> transformList(List<PenerimaanEntity> penerimaans) {
		List<PenerimaanModel> penerimaanModels = new ArrayList<PenerimaanModel>();
		for (PenerimaanEntity penerimaan : penerimaans) {
			penerimaanModels.add(transform(penerimaan));
		}
		return penerimaanModels;
	}

	private PenerimaanModel transform(PenerimaanEntity penerimaan) {
		List<PenerimaanDetailModel> penerimaanDetailModels = new ArrayList<PenerimaanDetailModel>();
		List<PenerimaanDetailEntity> penerimaanDetails = entityManager.getPenerimaanDetails(penerimaan);
		for (PenerimaanDetailEntity penerimaanDetail : penerimaanDetails) {
			BarangEntity barangEntity = penerimaanDetail.getBarang();
			GudangEntity gudangEntity = penerimaanDetail.getGudang();
			GudangModel gudang = new GudangModel(gudangEntity.getIdGudang(), gudangEntity.getNamaGudang(), gudangEntity.getLokasi(),
					gudangEntity.getKapasitas(), gudangEntity.getPetugas(), gudangEntity.getAlamat(), gudangEntity.getKode(), gudangEntity.getNotelp(),
					gudangEntity.getTgl_jth_tempo(), gudangEntity.getCreatedBy());
			SatuanModel satuan = new SatuanModel(barangEntity.getSatuan().getIdSatuan(), barangEntity.getSatuan().getSatuan(), barangEntity.getSatuan().getCreatedBy());
			JenisBarangEntity jenisBarangEntity = barangEntity.getJenisBarang();
			JenisBarangModel jenisBarang = new JenisBarangModel(jenisBarangEntity.getIdJenisBarang(), jenisBarangEntity.getJenis(), jenisBarangEntity.getCreatedBy());
			BarangModel barang = new BarangModel(barangEntity.getIdBarang(), barangEntity.getKdBarang(), jenisBarang, satuan, barangEntity.getNama(),
					barangEntity.getHargaBeli(), barangEntity.getHargaJual(), barangEntity.getCreatedBy());
			PenerimaanDetailModel penerimaanDetailModel = new PenerimaanDetailModel();
			penerimaanDetailModel.setIdDetailPenerimaan(penerimaanDetail.getIdDetailPenerimaan());
			penerimaanDetailModel.setBarang(barang);
			penerimaanDetailModel.setGudang(gudang);
			penerimaanDetailModel.setBulanKadaluarsa(penerimaanDetail.getBulanKadaluarsa());
			penerimaanDetailModel.setNoKontainer(penerimaanDetail.getNoKontainer());
			penerimaanDetailModel.setKeterangan(penerimaanDetail.getKeterangan());
			penerimaanDetailModel.setQty(penerimaanDetail.getQty());
			penerimaanDetailModels.add(penerimaanDetailModel);
		}
		return new PenerimaanModel(penerimaan.getIdPenerimaan(), penerimaan.getNomorPenerimaan(), penerimaan.getIdPembelian(), penerimaan.getTanggal(),
				penerimaan.getKeterangan(), penerimaan.getNomorPo(), new HashSet<PenerimaanDetailModel>(penerimaanDetailModels), penerimaan.getCreatedBy(), penerimaan.getCreatedDate());
	}

}
